package com.gui;
/**
* @author Dror
*
* email: gumjum.o.o@gmail.com
*
*/
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import com.fileio.FileIO;
import com.fish.World;
public class SimulationMenu extends Menu implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -5338452564016194010L;
JLabel statusLabel;
JButton startButton;
JButton stopButton;
JButton resetButton;
JButton loadButton;
JButton saveButton;
JButton backButton;
JFileChooser fc;
public SimulationMenu() {
super();
fc = new JFileChooser();
statusLabel = new JLabel();
statusLabel.setFocusable(false);
statusLabel.setBackground(lvl2);
// statusLabel.setPreferredSize(new Dimension(200,200));
startButton = new JButton("Start");
startButton.setFocusable(false);
startButton.setBackground(lvl2);
startButton.addActionListener(this);
stopButton = new JButton("Stop");
stopButton.setFocusable(false);
stopButton.setBackground(lvl2);
stopButton.addActionListener(this);
resetButton = new JButton("Reset");
resetButton.setFocusable(false);
resetButton.setBackground(lvl2);
resetButton.addActionListener(this);
loadButton = new JButton("Load");
loadButton.setFocusable(false);
loadButton.setBackground(lvl2);
loadButton.addActionListener(this);
saveButton = new JButton("Save");
saveButton.setFocusable(false);
saveButton.setBackground(lvl2);
saveButton.addActionListener(this);
backButton = new JButton("back");
backButton.setFocusable(false);
backButton.setBackground(lvl1);
backButton.addActionListener(this);
Container c = new Container();
c.setLayout(new GridLayout(5,0));
c.add(startButton);
c.add(stopButton);
c.add(resetButton);
c.add(loadButton);
c.add(saveButton);
this.setFocusable(false);
this.setBackground(bg12);
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setVerticalGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(backButton))
.addGroup(layout.createSequentialGroup()
.addComponent(c)
)
.addComponent(statusLabel));
layout.linkSize(statusLabel,c);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(backButton))
.addGroup(layout.createParallelGroup()
.addComponent(c)
)
.addComponent(statusLabel));
}
public void paintComponent(Graphics g){
labelUpdate();
super.paintComponent(g);
}
public void labelUpdate(){
String s = "<html><body>";
s += "--------------------------------------------------<br><br>";
s += "Simulation name: " + "fish" + "<br>";
s += "Simulation state: " + World.sim_stat + "<br>";
s += "Simulation fps: " + mp.fps + "<br>";
s += "Simulation 3d-objects: " + World.shapes.size() + "<br>";
s += "Simulation objects: " + World.sim_shapes.size() + "<br>";
s += "--------------------------------------------------<br>";
s += "</body></html>";
statusLabel.setText(s);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if(obj == startButton){
World.startSimulation();
}else if(obj == stopButton){
World.stopSimulation();
}else if(obj == resetButton){
World.resetSimulation();
}else if(obj == loadButton){
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("Opening: " + file.getPath() + ".");
World.fromXML(FileIO.readXMLFile(file.getPath()));
} else {
System.out.println("Open command cancelled by user.");
}
}else if(obj == saveButton){
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would save the file.
System.out.println("Saving: " + file.getName() + "." );
FileIO.writeXMLFile(World.toXML(), file);
} else {
System.out.println("Save command cancelled by user." );
}
}else if(obj == backButton){
mp.layout.replace(mp.curMenu, mp.mainMenu);
mp.curMenu = mp.mainMenu;
}
}
}